Explain Binary Search Tree in brief? | Data Structure Tutorial
In this article, you will learn about the Binary Search Tree in brief.
A binary search tree is a data structure that maintains a sorted list of numbers.
- It is known as a binary tree because each tree node has a maximum of two children.
- It is also known as a search tree because it is used to search for the presence of a number in 0(log(n)) time.
The qualities that separate a binary search tree from a basic binary tree are:
- All the nodes of the left subtree are less than the root node.
- All the nodes of the right subtree are more than the root node.
- Both the subtrees of each node are also binary search trees if they have both the above two properties.
The two basic operations that can be performed on a binary search tree are:
Search Operation: The property of BST creates the algorithm in which each left subtree has values less than the root and the values of the right subtree are greater than the root. To find the element in the binary search tree we have to follow the following steps.
- Check the value of the element.
- If the value of the element is less than the root value, then it is definitely in the left subtree.
- If the value of the element is greater than the root value, then it is definitely in the right subtree.
- If it is not present in any of the subtrees, then the element is not present in the tree.
Insert Operation: Inserting a value in an absolute position is kind of similar to searching because of the rule followed by the binary search tree i.e the value of the left subtree is lesser than the root and the value of the right subtree element is greater than the root. We keep searching the values on the right and left subtree and when we reach a point left and right is NULL, then we place a new node there. We have to attach the node with the parent node without doing any damage to the rest of the tree. This is where the return node; at the end comes in handy. In the case of NULL, the newly created node is returned and attached to the parent node, otherwise, the same node will be returned without a change when we go up until we return to the root.
Delete Operation:
In a binary search tree, there are three ways in which we can delete the node:
CASE I: In this case, it simply deletes the leaf node. The node which is to be deleted is the leaf node.
CASE II: In this case, the node to be deleted has a single child node. The steps of this case are:
- Replace that node with its child node.
- Remove the child node from its original position.
CASE III: In this case, the node to be deleted has two children. The steps of this case are:
- Get the inorder successor of that node.
- Replace the node with the inorder successor.
- Remove the inorder successor from its actual position.
Applications of Binary Search Tree
- It helps in dynamic sorting.
- For managing virtual memory areas in Unix Kernel.
- It is used in multilevel indexing in the database.


